package com.projectx.action.contact;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.projectx.R;

import java.util.List;

import static com.projectx.action.contact.CreateGroupActivity.editText;
import static com.projectx.action.contact.CreateGroupActivity.recyclerView_horizontal;
import static com.projectx.action.contact.CreateGroupActivity.windowManager;

public class CreateGroupAdpter extends RecyclerView.Adapter<CreateGroupAdpter.VH> {

    private Context context;
    private List<String> data;
    private  List<Bitmap>bitmaps;
    private AdapterView.OnItemClickListener mClickListener;
    protected static int[] count=new int[100000];
    public CreateGroupAdpter(Context context, List<String> data, List<Bitmap>bitmaps) {
        this.context = context;
        this.data = data;
        this.bitmaps=bitmaps;
    }

    @NonNull
    @Override
    public CreateGroupAdpter.VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.activity_delete_item, parent, false);
        return new CreateGroupAdpter.VH(view);
    }
    public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
        this.mClickListener = listener;
    }
    @Override
    public void onBindViewHolder(@NonNull final CreateGroupAdpter.VH holder, final int position) {
        holder.textView.setText(data.get(position));
        try {
            holder.imageView.setImageBitmap(bitmaps.get(position));
            if(bitmaps.get(position).equals(null)){
                holder.imageView.setImageResource(R.drawable.ic_account_circle_black_36dp);
            }
        }catch (NullPointerException e){
            holder.imageView.setImageResource(R.drawable.ic_account_circle_black_36dp);
        }catch (IndexOutOfBoundsException e){
            Log.e("lzw","头像list越界了");
        }
        holder.click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                count[position]++;
                if (count[position]%2!=0){
                    holder.check.setVisibility(View.VISIBLE);
                    HorizontalAdapter.Position.add(position);
                    //data.add("");
                    CreateGroupActivity.recyclerView_horizontal.getAdapter().notifyDataSetChanged();
                    //horizontal_adapter.notifyItemInserted(horizontal_adapter.getItemCount()-1);
                    if (recyclerView_horizontal.getAdapter().getItemCount()>=6){
                        LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) recyclerView_horizontal.getLayoutParams();
                        linearParams.width=(windowManager.getDefaultDisplay().getWidth())*6/10;
                    }else {
                        LinearLayout.LayoutParams linearParams1 =(LinearLayout.LayoutParams) editText.getLayoutParams();
                        linearParams1.width=windowManager.getDefaultDisplay().getWidth()*(10-recyclerView_horizontal.getAdapter().getItemCount())/10;
                        LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) recyclerView_horizontal.getLayoutParams();
                        linearParams.width=windowManager.getDefaultDisplay().getWidth()*recyclerView_horizontal.getAdapter().getItemCount()/10;
                    }
                   Log.d("lzw","数据呢");
                }
                else{
                    holder.check.setVisibility(View.GONE);
                    //data.remove(position);
                    HorizontalAdapter.Position.remove((Integer) position);
                   // Horizontal_Adapter horizontal_adapter=new Horizontal_Adapter(context,bitmaps,position,false,data);
                    CreateGroupActivity.recyclerView_horizontal.getAdapter().notifyDataSetChanged();
                    if (recyclerView_horizontal.getAdapter().getItemCount()>=6){
                        LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) recyclerView_horizontal.getLayoutParams();
                        linearParams.width=(windowManager.getDefaultDisplay().getWidth())*6/10;
                    }else {
                        LinearLayout.LayoutParams linearParams1 =(LinearLayout.LayoutParams) editText.getLayoutParams();
                        linearParams1.width=windowManager.getDefaultDisplay().getWidth()*(10-recyclerView_horizontal.getAdapter().getItemCount())/10;
                        LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) recyclerView_horizontal.getLayoutParams();
                        linearParams.width=windowManager.getDefaultDisplay().getWidth()*recyclerView_horizontal.getAdapter().getItemCount()/10;
                    }
                   // horizontal_adapter.notifyDataSetChanged();
                }
                // 点击事件
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class VH extends RecyclerView.ViewHolder {

        TextView textView;
        ImageView imageView,check;
        View click;
        public VH(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text);
            imageView= itemView.findViewById(R.id.imageView);
            click=itemView.findViewById(R.id.click);
            check=itemView.findViewById(R.id.check);
        }
    }
}
